package com.finalcial.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.finalcial.po.LoginMsg;
import com.finalcial.po.OtherInformation;
import com.finalcial.service.ApplicationService;
import com.finalcial.service.ReadService;

@Controller
@RequestMapping("/propertyManagerApplication")//访问物业管理申请界面
public class PropertyManagerApplicationController {
	

	@Resource
	private ApplicationService applicationService;
	@Resource
	private ReadService readService;
	
	//进入物业管理申请界面--显示信息
	@RequestMapping("/propertyManagerBasic")
	public ModelAndView propertyManagerApplicationMessage(HttpSession httpSession){
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId =  loginMsg.getUserId();
		
		//显示所有可选择的接收者角色
		List<Map<String, Object>> roleNameList = applicationService.showRoleNameList();
		//显示申请人基本信息
		List<Map<String,Object>> invoiceTypeList = applicationService.showInvoiceTypeName();//票据类型信息
		List<Map<String, Object>> basicList = applicationService.showBasicList(userId);//申请人基本信息
		//显示内部人信息
		OtherInformation otherMessage = applicationService.showInsiderInfoOther(userId);
		ModelAndView mav = new ModelAndView();
		mav.addObject("roleNameList", roleNameList);
		mav.addObject("invoiceTypeList", invoiceTypeList);
		mav.addObject("basicList", basicList);
		mav.addObject("otherMessage", otherMessage);
		mav.setViewName("expenseplatform/apply/propertyManagerApply");//访问公务交通申请表
		return mav;
	}
	
	/**
	 * 点击角色--显示用户下拉框信息
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/listUserNameAll", method = RequestMethod.POST)
	public List<Map<String, Object>> listUserNameAll(HttpServletRequest request, HttpServletResponse response) {
		String roleId = request.getParameter("roleId");
		List<Map<String, Object>> userNameList = applicationService.listUserNameAll(Integer.valueOf(roleId));
		for(Map<String, Object> map:userNameList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		return userNameList;
	}
	
			//点击用户--显示记账成本中心
			@ResponseBody 
			@RequestMapping(value="/getDeptName", method = RequestMethod.POST)
			public HashMap<String, Object> getDeptName(HttpServletRequest request, HttpServletResponse response) {
				Integer userId = Integer.valueOf(request.getParameter("userId"));
				//System.out.println(userId);
				String deptName = applicationService.getUserDepartmentName(userId);
				//System.out.println(deptName);
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("deptName", deptName);
				return map;
				
			}
			
			//新增物业管理报销申请信息
			@ResponseBody
			@RequestMapping(value="/insertPropertyManagerMsg", method = RequestMethod.POST)
			public Integer insertPropertyManagerMsg(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession) {
				
				Date invoiceTime = null;
				Date reimbursementTime = null;
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟  
				Integer accepter = Integer.parseInt(request.getParameter("userId"));//接收者id
				Integer invoiceTypeId  = Integer.parseInt((request.getParameter("invoiceTypeId")).trim());
				String reimbursementId  = request.getParameter("reimbursementId");
				try {
					 reimbursementTime  =sdf.parse(request.getParameter("reimbursementTime"));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String reimbursementExplain  = request.getParameter("reimbursementExplain");
				String payments  = request.getParameter("payments");
				try {
					 invoiceTime  = sdf.parse(request.getParameter("invoiceTime"));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				String isAdvance  = request.getParameter("isAdvance");
				Integer reimbursementNumber  = Integer.parseInt(request.getParameter("reimbursementNumber"));
				String isCostCapitalBudget  = request.getParameter("isCostCapitalBudget");
				Integer reimbursementMoney  = Integer.parseInt(request.getParameter("reimbursementMoney"));
				Integer reimbursementMoneyOfTax  = Integer.parseInt(request.getParameter("reimbursementMoneyOfTax"));
				String isSingle = request.getParameter("isSingle");
				LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
				Integer userId = loginMsg.getUserId();	
				
				System.out.println(accepter+" "+invoiceTypeId+" "+reimbursementId+" "+reimbursementTime+" "+reimbursementExplain+" "+payments+" "+invoiceTime+" "+isAdvance+" "+" "+reimbursementNumber+" "+isCostCapitalBudget+" "+reimbursementMoney+" "+reimbursementMoneyOfTax+" "+userId+" "+isSingle);
				Integer state = applicationService.insertManagerApplicationMsg(userId, accepter, invoiceTypeId, reimbursementId, reimbursementTime, reimbursementExplain, payments, invoiceTime, isAdvance, reimbursementNumber, isCostCapitalBudget, reimbursementMoney, reimbursementMoneyOfTax, isSingle);
				return state;
				
			}

}
